package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.cross.CrossBanquetData;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.banquet.CrossBanquetConfigMgr;
import com.yanqu.road.server.manager.banquet.CrossBanquetMgr;
import com.yanqu.road.server.manager.banquet.CrossPartyMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = CrossProtocol.C_CROSS_PARTY_SEARCH,desc = "")
public class CrossSearchPartyCmd extends CrossCommand {
    private long searchUserId;
    private long userId;
    private long searchServerId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BanquetProto.PlayerBanquetReq reqMsg = BanquetProto.PlayerBanquetReq.parseFrom(packet.getBytes());
        searchUserId = reqMsg.getUserId();
        userId = packet.getPlayerId();
        if (reqMsg.hasServerId()) {
            searchServerId = reqMsg.getServerId();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        //两个宴会，一个本服私宴一个跨服私宴
        BanquetProto.PlayerBanquetResp.Builder respMsg = BanquetProto.PlayerBanquetResp.newBuilder();
        CrossBanquetData userBanquetData = CrossPartyMgr.getUserParty(searchServerId,searchUserId);
        if (userBanquetData != null) {
            boolean selfJoin = CrossPartyMgr.isSelfJoin(userId, userBanquetData.getBanquetId());
            BanquetProto.PlayerBanquetTemp.Builder respServerTemp = BanquetProto.PlayerBanquetTemp.newBuilder();
            respServerTemp.setBanquetId(userBanquetData.getBanquetId());
            respServerTemp.setSelfJoin(selfJoin);
            BanquetInfo banquetInfo = CrossBanquetConfigMgr.getBanquetInfo(userBanquetData.getType());
            respServerTemp.setBanquetType(banquetInfo.getType1());
            respMsg.addBanquets(respServerTemp);
        }
        CrossBanquetData userCrossBanquetData = CrossBanquetMgr.getUserBanquet(searchUserId);
        if (userCrossBanquetData != null) {
            boolean selfJoin = CrossBanquetMgr.isSelfJoin(userId, userCrossBanquetData.getBanquetId());
            BanquetProto.PlayerBanquetTemp.Builder respCrossTemp = BanquetProto.PlayerBanquetTemp.newBuilder();
            respCrossTemp.setBanquetId(userCrossBanquetData.getBanquetId());
            respCrossTemp.setSelfJoin(selfJoin);
            BanquetInfo banquetInfo = CrossBanquetConfigMgr.getBanquetInfo(userCrossBanquetData.getType());
            respCrossTemp.setBanquetType(banquetInfo.getType1());
            respMsg.addBanquets(respCrossTemp);
        }
        if (respMsg.getBanquetsList().size() == 0) {
            ret = GameErrorCode.E_BANQUET_NORMAL_PLAYER_NO;
        }
        respMsg.setRet(ret);
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_BANQUET_PLAYER_GET, respMsg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
